using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct MyLayerMask
{
    public int value { get; private set; }
    public string bitStr { get { return System.Convert.ToString(value, 2); } }

    public MyLayerMask(int defaultValue = 0) 
    {
        value = defaultValue;
    }

    public void Add(int value)
    {
        this.value |= value;
    }

    public void Remove(int value)
    {
        this.value &= ~value;
    }

    public bool Contains(int index) 
    {
        return bitStr.Length > index && bitStr[bitStr.Length - 1 - index] == '1';
    }

    public static List<int> GetContainLayer(int value, int enumCount) 
    {
        string bitStr = string.Empty;
        if (value == -1) 
        {
            bitStr = System.Convert.ToString(GetLayerMaxValue(enumCount), 2);
        }
        bitStr = System.Convert.ToString(value, 2);
        List<int> layers = new List<int>();
        for (int i = 0; i < bitStr.Length; i++) 
        {
            if(bitStr[i] == '1') 
            {
                layers.Add((int)Mathf.Pow(2, bitStr.Length - 1 - i));
            }
        }
        return layers;
    }

    public static int GetLayerMaxValue(int count) 
    {
        int value = 0;
        for (int i = 0; i < count; i++) 
        {
            value += (int)Mathf.Pow(2, i);
        }
        return value;
    }
}
